package com.example.wechat;

import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class MomentsAdapter extends RecyclerView.Adapter<MomentsAdapter.ViewHolder> {
    private List<Moment> moments;
    public MomentsAdapter(List<Moment> moments) {
        this.moments = moments;
    }
    public void setData(List<Moment> newData) {
        this.moments = newData;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_moment, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Moment moment = moments.get(position);
        holder.tvNickname.setText("用户" + moment.getPublisherId());
        holder.tvContent.setText(moment.getContent());
        if (!TextUtils.isEmpty(moment.getImagePath())) {
            holder.imgMoment.setVisibility(View.VISIBLE);
            holder.imgMoment.setImageURI(Uri.parse(moment.getImagePath()));
        } else {
            holder.imgMoment.setVisibility(View.GONE);
        }
        holder.tvLikes.setText("点赞: " + moment.getLikesCount());
        holder.tvComments.setText("评论: " + moment.getCommentsCount());
    }
    @Override
    public int getItemCount() {
        return moments == null ? 0 : moments.size();
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvNickname, tvContent, tvLikes, tvComments;
        ImageView imgMoment;
        ViewHolder(View itemView) {
            super(itemView);
            tvNickname = itemView.findViewById(R.id.tv_nickname);
            tvContent = itemView.findViewById(R.id.tv_content);
            imgMoment = itemView.findViewById(R.id.img_moment);
            tvLikes = itemView.findViewById(R.id.tv_likes);
            tvComments = itemView.findViewById(R.id.tv_comments);
        }
    }
} 